package org.jfteam.mybatis.executor;

import org.apache.ibatis.session.SqlSession;
import org.jfteam.common.exception.SystemException;
import org.jfteam.mybatis.util.MyBatisUtils;

/**
 * Created with IntelliJ IDEA.
 * Description: 描述
 * Author:      fengwenping
 * Date:        2021/3/28 17:56
 */
public class JdbcOperation<T,R> {

    public R execute(Class<T> tClass, JdbcCallBack<T,R> jdbcCallBack) {
        R result = null;
        final SqlSession sqlSession = MyBatisUtils.openSqlSession(false);
        final T mapper = sqlSession.getMapper(tClass);
        try {
            result = jdbcCallBack.execute(mapper);
            MyBatisUtils.commit(sqlSession);
        } catch (SystemException e) {
            MyBatisUtils.rollback(sqlSession);
        } finally {
            MyBatisUtils.closeSqlSession(sqlSession);
        }
        return result;
    }
}
